using System.Runtime.InteropServices;
using System.Security;
using Microsoft.Win32.SafeHandles;

namespace Be.Timvw.Framework.Win32.SafeHandles
{
    public class SafeGlobalAllocUnicodeHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        protected SafeGlobalAllocUnicodeHandle()
        : base(true)
        {
        }

        public SafeGlobalAllocUnicodeHandle(SecureString s)
            : this()
        {
            this.SetHandle(Marshal.SecureStringToGlobalAllocUnicode(s));
        }

        protected override bool ReleaseHandle()
        {
            if(!this.IsInvalid)
            {
                Marshal.ZeroFreeGlobalAllocUnicode(this.handle);
            }

            return true;
        }
    }
}